package java_util.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class C_WebClientThread implements Runnable {

    static Map<String, I_WebCallback> o_eventMap = new HashMap<>();
    private float requestDelay = (float) 0.5;//

    private Socket _s;
    private BufferedReader i;
    private PrintStream o;

    public C_WebClientThread(Socket s) {
        this._s = s;
    }

    public void run() {
        try {
            this.i = new BufferedReader(new InputStreamReader(this._s.getInputStream()));
            this.o = new PrintStream(this._s.getOutputStream());
            String request = this.getValidRequest();
            if (!request.equals("")) {
                System.out.print("当前虚拟机最大可用内存为:");
                System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024 + "M");
                System.out.print("当前，虚拟机已占用内存:");
                System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024 + "M");
                this.o.println("HTTP/1.1 200 OK");
                Date now = new Date();
                this.o.println("Data:" + now);
                this.o.println("Server: JHServer");
                this.o.println("Access-Control-Allow-Origin:*");

                this.o.println("Content-Type: text/html; charset=UTF-8");
                this.o.println();
                String content = "null牛逼无敌的数据..................的速度及所得税";
//                IndeMatchInfo imi = new IndeMatchInfo(22,22,2,2,22,(double)0.0,"S7-200");
//                content = imi.getHTML();
//
//                imi.clear();
//                imi = null;
                System.out.print("当前虚拟机最大可用内存为:");
                System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024 + "M");
                System.out.print("当前，虚拟机已占用内存:");
                System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024 + "M");

                this.o.println(content);
                content = "";
                this.o.flush();

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            this.close();
        }

    }

    boolean isGo = true;

    private String getValidRequest() {
        float second = (float) 0.0;
        String request = null;
        try {
            while (!this.i.ready()) {
                second += 0.01;
                if (second > this.requestDelay) {
                    System.out.println("一个客户端延迟  " + this._s);
                    isGo = false;
                    break;
                }
                Thread.sleep(10);
            }

            if (isGo) {
                request = this.i.readLine();
                System.out.println("信息: " + request);
                if (request.contains("GET /") && request.contains(" HTTP/")) {
                    request = request.substring(request.indexOf("/"), request.indexOf(" HTTP/"));
                    S_msgHandle(request, true);
                } else {
                    isGo = false;
                }
            }
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        } finally {
            if (isGo) {
                return request;
            } else {
                return "null";
            }
        }
    }

    void S_msgHandle(String request, boolean isGet) {
        System.out.println("客户端请求信息: " + request);
        String[] sss = request.split("[?]");
        String key = sss[0].trim();
        if (o_eventMap.containsKey(key)) {
            System.out.println("dddddddddddddddd: " + request);
        }
        Map<String, String> data = new HashMap<>();

        String[] sss2 = sss[1].split("[&]");
        for (String ss : sss2) {
            String[] sss3 = ss.split("[=]");
            if (sss3.length >= 2) {
                data.put(sss3[0].trim(), sss3[1].trim());
            }
        }
        I_WebCallback callback = o_eventMap.get(key);
        if (isGet) {
            callback.S_doGet(data);
        }
    }

    private void close() {
        try {
            if (this.i != null) {
                this.i.close();
                this.i = null;
            }
            if (this.o != null) {
                this.o.close();
                this.o = null;
            }
            if (this._s != null) {
                this._s.close();
                this._s = null;
            }
            System.out.println("一个客户端已断开连接...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}